package com.common.library.ui.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.animation.AccelerateInterpolator;
import android.widget.TextView;

/**
 * @author txf
 * @Title 可让文字逐个显示的 TextView
 * @package com.common.library.ui.view
 * @date 2017/6/8 0008
 */
public class ShadeTextView extends TextView {
    private float mProgress;
    private int mTextWidth;

    public ShadeTextView(Context context) {
        super(context, null);
    }

    public ShadeTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mTextWidth = w;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save(Canvas.CLIP_SAVE_FLAG);
        canvas.clipRect(0, 0, mProgress * mTextWidth, getMeasuredHeight());
        super.onDraw(canvas);
    }

    public void startAnim(long duration) {
        ObjectAnimator anim = ObjectAnimator.ofFloat(this, "progress", 0, 1).setDuration(duration);
        anim.setInterpolator(new AccelerateInterpolator());
        anim.start();
    }

    public void setProgress(float progress) {
        this.mProgress = progress;
        invalidate();
    }

    public float getProgress() {
        return mProgress;
    }
}
